<template>
  <div id="app">
<!--    <audio id="audio" src="https://www.caihengyun.com/chyimg/audio/repair.mp3"></audio>-->
<!--    <audio id="audio1" src="https://www.caihengyun.com/chyimg/audio/property.mp3"></audio>-->
    <router-view />
  </div>
</template>

<script>

import {getBySysUserId} from "@/api/module/estate/callCenter";

export default  {
  name:  'App',
  created() {
    this.initData();
    this.localSocket();
    this.beiEnCallSocket();
  },
  data() {
    return {
      // 版本号
      userType: 0,
    };
  },
  methods:{
    initData(){
      console.log('this.$store.state.user.',this.$store.state.user)
      this.userType = this.$store.state.user.user_type;
      console.log(' this.data.userType',  this.userType)
      // if (this.userType == 3){
      //   this.localSocket();
      //   this.beiEnCallSocket();
      // }
    },
    // socket 初始化
    localSocket(){
      this.userType = this.$store.state.user.user_type;
      console.log('socket init');
      const that = this;
      if (typeof (WebSocket) == 'undefined') {
        that.$notify({
          title: '提示',
          message: '当前浏览器无法接收实时信息，请使用谷歌浏览器！',
          type: 'warning',
          duration: 0
        });
      }else{
        const ws = new WebSocket(process.env.VUE_APP_WS);
        that.$socket.setWs(ws);
        // console.log('wxss',process.env.VUE_APP_WS);
        // 监听socket打开
        that.$socket.ws.onopen = function () {
          var timestamp = Date.parse(new Date());
          that.$socket.ws.send("connect|" + timestamp)
        };
        // 监听socket消息接收
        that.$socket.ws.onmessage = function (msg) {
          console.log('socket.onmessage',msg);
          // 转换为json对象
          if(msg.data!=='connect'){
            var dataJson = JSON.parse(msg.data);
            console.log('dataJson',dataJson);
            // if(dataJson.type === 'repairRecord'){
            //   that.$notify.info({
            //     title: '报修消息提醒',
            //     message: "有新报修信息，请及时处理"
            //   });
            //   var myMp3 = document.getElementById("audio");
            //   myMp3.play();
            // }
            // else if(dataJson.type === 'property'){
            //   that.$notify.info({
            //     title: '物业交费申请消息提醒',
            //     message: "有新物业交费申请信息，请及时处理"
            //   });
            //   var myMp3 = document.getElementById("audio1");
            //   myMp3.play();
            // }
          }
        };
        // 监听socket错误
        that.$socket.ws.onerror = function () {
          that.$notify({
            title: '错误',
            message: '服务器错误，无法接收实时信息',
            type: 'error',
            duration: 0
          });
        };
        // 监听socket关闭
        that.$socket.ws.onclose = function () {
          console.log('WebSocket已关闭');
        }
      }
    },
    beiEnCallSocket(){
      console.log('beiEnCallSocket socket init');
      const that = this;
      if (typeof (WebSocket) == 'undefined') {
        that.$notify({
          title: '提示',
          message: '当前浏览器无法接收实时信息，请使用谷歌浏览器！',
          type: 'warning',
          duration: 0
        });
      }else{
        getBySysUserId().then(response => {
          let center = response.data;
          console.log('response', response)
          if (response.code == 200 && center != undefined) {
            // center.wsUrl
            const wsCall = new WebSocket(center.wsUrl);
            that.$socket.setWsCall(wsCall);
            // 监听socket打开
            that.$socket.wsCall.onopen = function (msg) {
              console.log('socket.onopen',msg);
              that.$socket.wsCall.send("{\"req\":\"HP_Init\",\"rid\":1,\"para\":{\"Para\":\"0\"}}");
            };
            // 监听socket消息接收
            that.$socket.wsCall.onmessage = function (msg) {
              console.log('socket.onmessage',msg);
              // 转换为json对象
              // if(msg.data!=='connect'){
              //   var dataJson = JSON.parse(msg.data);
              //   console.log('dataJson',dataJson);
              // }
            };
            // 监听socket错误
            that.$socket.wsCall.onerror = function (res) {
              console.log('onerror', res)
              that.$notify({
                title: '错误',
                message: '服务器错误，无法接收实时信息',
                type: 'error',
                duration: 0
              });
            };
            // 监听socket关闭
            that.$socket.wsCall.onclose = function () {
              console.log('WebSocket已关闭');
            }
          }
        })
      }
    },
  },
  metaInfo() {
    return {
      title: this.$store.state.settings.dynamicTitle && this.$store.state.settings.title,
      titleTemplate: title => {
        return title ? `${title} - ${process.env.VUE_APP_TITLE}` : process.env.VUE_APP_TITLE
      }
    }
  }
}
</script>
